//
//  YRControlViewController.m
//  YRControlViewController
//
//  Created by 张常兴 on 15/11/13.
//  Copyright © 2015年 小怪兽. All rights reserved.
//

#define MAIN_SCROLLVIEW_HEIGHT  self.view.height - CONTROL_BUTTON_HEIGHT

#import "YRControlViewController.h"
#import "YRControlButton.h"

@interface YRControlViewController ()
<UIScrollViewDelegate>

@property (nonatomic, strong) YRControlButton *controlBtn;
@property (nonatomic, strong) UIScrollView    *mainScrollView;
@property (nonatomic,strong ) NSMutableSet    *loadedIndexSet;
@property (nonatomic,assign ) NSInteger       lastOffset;



@end

@implementation YRControlViewController

- (instancetype) init {
    self = [super init];
    if (self) {
        _loadedIndexSet = [NSMutableSet set];
//        [self loadUI];

    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark ------------------ 初始化UI
- (void)loadUI {
    
    NSMutableArray *titleArray = [@[] mutableCopy];
    for (UIViewController *vc in _viewControllersArray) {
        [self addChildViewController:vc];
        [titleArray addObject:vc.title];
    }
    if (!_controlBtn) {
        _controlBtn = [[YRControlButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CONTROL_BUTTON_HEIGHT)];
    }
    
    [_controlBtn initButtonWithTitles:titleArray icon:nil iconSelected:nil isShowSeparator:YES isShortSelector:NO setButtonBackground:WHITE_COLOR setButtonSelectedBackground:WHITE_COLOR defaultIndex:_currentScrollIndex];
    
    __weak YRControlViewController *wself = self;
    _controlBtn.block = ^(NSInteger index){
        [wself loadViewAtIndex:index];
        CGFloat x = FULL_WIDTH *index;
        CGRect rect = CGRectMake(x, 0, FULL_WIDTH, MAIN_SCROLLVIEW_HEIGHT);
        [wself.mainScrollView scrollRectToVisible:rect animated:NO];
        wself.lastOffset = x;
    };
    
    [self.view addSubview:_controlBtn];
    [self addScrollView];
    
}


#pragma mark ------------------ 创建scrollView
- (void)addScrollView {
    _mainScrollView = [[YPBaseScrollView alloc]initWithFrame:CGRectMake(0, CONTROL_BUTTON_HEIGHT, FULL_WIDTH, MAIN_SCROLLVIEW_HEIGHT)];
    _mainScrollView.backgroundColor = DEFAULT_BG_COLOR;
    _mainScrollView.delegate = self;
//    _mainScrollView.bounces = NO;
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.scrollsToTop = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.contentSize = CGSizeMake(FULL_WIDTH*_viewControllersArray.count, MAIN_SCROLLVIEW_HEIGHT);
    _mainScrollView.contentOffset = CGPointMake(FULL_WIDTH *_currentScrollIndex, 0);
    
    [self addScrollViewContentAtIndex:_currentScrollIndex];
    
    [self.view addSubview:_mainScrollView];
}

#pragma mark ------------------ 添加子VC
- (void)addScrollViewContentAtIndex:(NSInteger)index {
    UIViewController *vc = _viewControllersArray[index];
    vc.view.frame = CGRectMake(FULL_WIDTH * index, 0, FULL_WIDTH, MAIN_SCROLLVIEW_HEIGHT);
    [_mainScrollView addSubview:vc.view];
}

#pragma mark ------------------ 加载对应的VC
- (void)loadViewAtIndex:(NSInteger)index {
    if (index != _currentScrollIndex && 0 <= index && index < _viewControllersArray.count && ![_loadedIndexSet containsObject:[NSString stringWithFormat:@"%ld",(long)index]]) {
        [_loadedIndexSet addObject:[NSString stringWithFormat:@"%ld",(long)index]];
        [self addScrollViewContentAtIndex:index];
    }
}



#pragma mark ------------------ UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger x = scrollView.contentOffset.x;
    NSInteger index;
    NSInteger fullWidth = FULL_WIDTH;
    NSInteger remainder = x%fullWidth;
    if (x > _lastOffset && x < FULL_WIDTH *(_viewControllersArray.count - 1) && remainder >= TRIGGER_OFFSET ) {
        index = x/fullWidth+1;
        [self loadViewAtIndex:index];
        if (index >= 0 && index < _viewControllersArray.count) {
            [_controlBtn selectAtIndex:index];
        }
    }
    
    if (x < _lastOffset && x > 0  && remainder <= FULL_WIDTH-TRIGGER_OFFSET ) {
        index = x/fullWidth;
        [self loadViewAtIndex:index];
        if (index >= 0 && index < _viewControllersArray.count) {
            [_controlBtn selectAtIndex:index];
        }
    }
    _lastOffset = scrollView.contentOffset.x;
    
}

#pragma mark ------------------ 初始化数组
- (void)setViewControllersArray:(NSMutableArray *)viewControllersArray {
    _viewControllersArray = viewControllersArray;
    [self loadUI];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
